package com.ruoyi.web.controller.laiqianba.merchant;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.laiqianba.enums.CheckStatusEnum;
import com.ruoyi.laiqianba.enums.InfoTypeEnum;
import com.ruoyi.laiqianba.enums.PostInfoEnum;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.MerchantBaseInfo;
import com.ruoyi.laiqianba.service.IAdminHttpService;
import com.ruoyi.merchant.enums.ShowPositionEnum;
import com.ruoyi.merchant.service.IMerchantBaseInfoService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.service.IMerchantService;
import com.ruoyi.web.wrapper.MerchantBaseInfoWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 商户基本信息Controller
 *
 * @author ruoyi
 * @date 2023-08-12
 */
@RestController
@RequestMapping("/merchant/baseInfo")
public class MerchantBaseInfoController extends BaseController {
    @Autowired
    private IMerchantBaseInfoService merchantBaseInfoService;

    @Autowired
    private IMerchantService iMerchantService;
    @Autowired
    ISysUserService sysUserService;

    @GetMapping("/extData")
    public AjaxResult extData()
    {
        JSONObject returnData = new JSONObject();
        // 商务列表信息
        // 单独的个人还是，整体的商务列表
        returnData.put("ownerInfoList", sysUserService.getUserInfoByPostId(PostInfoEnum.MERCHANT_MANAGER.getPostId()));
        returnData.put("showPositionList", ShowPositionEnum.getMapList());
        returnData.put("sourceList", InfoTypeEnum.getMapList());
        returnData.put("checkTypeList", CheckStatusEnum.getMapList());

        String shareUrl = StrUtil.format("http://apply.honglaikeji.cn/{}", SecurityUtils.getUserId());
        // 商务推广链接
        returnData.put("shareUrl", shareUrl);

        return AjaxResult.success(returnData);
    }

    /**
     * 查询商户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:baseInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerchantBaseInfo merchantBaseInfo) {
        Long userId = getUserId();
        if (!SecurityUtils.loginUserIsAdmin()){
            merchantBaseInfo.setOwnerId(userId);
        }
        startPage();
        List<Map<String, Object>> list = merchantBaseInfoService.selectMerchantBaseInfoMapList(merchantBaseInfo);
        list = new MerchantBaseInfoWrapper(list).wrap();
        return getDataTable(list);
    }

    /**
     * 导出商户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:baseInfo:export')")
    @Log(title = "商户基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MerchantBaseInfo merchantBaseInfo) {
        List<MerchantBaseInfo> list = merchantBaseInfoService.selectMerchantBaseInfoList(merchantBaseInfo);
        ExcelUtil<MerchantBaseInfo> util = new ExcelUtil<MerchantBaseInfo>(MerchantBaseInfo.class);
        util.exportExcel(response, list, "商户基本信息数据");
    }

    /**
     * 获取商户基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:baseInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(merchantBaseInfoService.selectMerchantBaseInfoById(id));
    }

    /**
     * 新增商户基本信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:baseInfo:add')")
    @Log(title = "商户基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantBaseInfo merchantBaseInfo) {
        return iMerchantService.insertMerchantBaseInfo(merchantBaseInfo);
    }

    /**
     * 修改商户基本信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:baseInfo:edit')")
    @Log(title = "商户基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantBaseInfo merchantBaseInfo) {
        return toAjax(merchantBaseInfoService.updateMerchantBaseInfo(merchantBaseInfo));
    }

    /**
     * 删除商户基本信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:baseInfo:remove')")
    @Log(title = "商户基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(merchantBaseInfoService.deleteMerchantBaseInfoByIds(ids));
    }

    /**
     * 更新机构状态
     *
     * @param request
     * @return
     */
    @PreAuthorize("@ss.hasPermi('merchant:baseInfo:updateStatus')")
    @Log(title = "商户基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody MerchantBaseInfo request) {
        return toAjax(merchantBaseInfoService.updateMerchantBaseInfo(request));
    }

    @Autowired
    IAdminHttpService adminHttpService;

    @PreAuthorize("@ss.hasPermi('merchant:baseInfo:pushManual')")
    @PostMapping("/pushManual")
    public AjaxResult pushManual(@RequestParam("infoId") String infoId,
                                 @RequestParam("uid") String uid) {
        return adminHttpService.merchantDataPushManual(infoId, uid);
    }



}
